<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>装箱查询</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
        }
        .header h1 {
            font-size: 24px;
            color: #333;
        }
        .scan-area {
            background-color: #fff;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .scan-input {
            width: 100%;
            padding: 10px;
            font-size: 18px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin-bottom: 10px;
        }
        .list-area {
            background-color: #fff;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .list-header {
            display: grid;
            grid-template-columns: 1fr 2fr 1fr;
            padding: 10px 0;
            border-bottom: 1px solid #ddd;
            font-weight: bold;
        }
        .list-item {
            display: grid;
            grid-template-columns: 1fr 2fr 1fr;
            padding: 10px 0;
            border-bottom: 1px solid #eee;
        }
        .alert-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }
        .alert-content {
            background-color: #333;
            padding: 30px;
            border-radius: 10px;
            text-align: center;
            color: #fff;
            width: 200px;
            position: relative;
        }
        .alert-icon {
            width: 50px;
            height: 50px;
            background-color: #fff;
            border-radius: 50%;
            margin: 0 auto 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 30px;
            color: #333;
        }
        .alert-text {
            font-size: 16px;
            line-height: 1.4;
        }
        .back-button {
            position: fixed;
            top: 20px;
            left: 20px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 8px 15px;
            cursor: pointer;
            display: flex;
            align-items: center;
            font-size: 16px;
            color: #333;
            z-index: 100;
        }
        .back-button:before {
            content: "←";
            margin-right: 5px;
            font-size: 18px;
        }
    </style>
</head>
<body>
    <button class="back-button" onclick="history.back()">返回</button>
    <div class="container">
        <div class="header">
            <h1>装箱查询</h1>
        </div>
        <div class="scan-area">
            <input type="text" class="scan-input" id="boxCodeInput" placeholder="请扫描箱码">
        </div>
        <div class="list-area">
            <div class="list-header">
                <div>条码</div>
                <div>货号/颜色/尺码</div>
                <div>数量</div>
            </div>
        </div>
    </div>
    <div class="alert-overlay" id="alertOverlay">
        <div class="alert-content">
            <div class="alert-icon">!</div>
            <div class="alert-text">请扫描正确的箱码</div>
        </div>
    </div>
    <script>
        // 硬编码 SKU 数据
        const skuData = {
            "boxcode": "CD250423000001",
            "skudetail": [
                {
                    "fnsku": "X001P67ABJ",
                    "chinese_sku": "ST1839/灰色/8",
                    "quantity": 9
                },
                {
                    "fnsku": "X001P58RN9",
                    "chinese_sku": "ST1839/白色/9",
                    "quantity": 8
                },
                {
                    "fnsku": "X001P59JSB",
                    "chinese_sku": "ST1839/灰白/9",
                    "quantity": 1
                }
            ]
        };

        function validateBoxCode(code) {
            // 检查箱码格式：CD + 日期(6位YYMMDD) + 序号(6位)
            const regex = /^CD\d{12}$/;
            if (!regex.test(code)) {
                return false;
            }

            // 提取日期部分 YYMMDD
            const dateStr = code.substring(2, 8);
            const year = 2000 + parseInt(dateStr.substring(0, 2)); // 假设年份在2000-2099之间
            const month = parseInt(dateStr.substring(2, 4));
            const day = parseInt(dateStr.substring(4, 6));

            // 检查日期是否有效
            const date = new Date(year, month - 1, day); // 月份从0开始，所以要减1
            return date.getFullYear() === year && 
                   date.getMonth() === month - 1 && 
                   date.getDate() === day &&
                   month >= 1 && month <= 12 && // 确保月份在1-12之间
                   day >= 1 && day <= 31; // 简单的日期检查
        }

        function showAlert(message) {
            const overlay = document.getElementById('alertOverlay');
            const alertText = overlay.querySelector('.alert-text');
            alertText.textContent = message;
            overlay.style.display = 'flex';
            
            setTimeout(() => {
                overlay.style.display = 'none';
            }, 1000);
        }

        function updateList(boxCode) {
            const listArea = document.querySelector('.list-area');
            // 保留表头
            listArea.innerHTML = `
                <div class="list-header">
                    <div>条码</div>
                    <div>货号/颜色/尺码</div>
                    <div>数量</div>
                </div>
            `;

            if (!skuData || skuData.boxcode !== boxCode) {
                return;
            }

            // 添加所有商品
            skuData.skudetail.forEach(item => {
                const listItem = document.createElement('div');
                listItem.className = 'list-item';
                listItem.innerHTML = `
                    <div>${item.fnsku}</div>
                    <div>${item.chinese_sku}</div>
                    <div>${item.quantity}</div>
                `;
                listArea.appendChild(listItem);
            });
        }

        // 箱码输入框事件监听
        document.getElementById('boxCodeInput').addEventListener('change', function(e) {
            const boxCode = e.target.value.trim();
            console.log('Box code scanned:', boxCode);
            
            if (!validateBoxCode(boxCode)) {
                showAlert('箱码不存在');
                e.target.value = ''; // 清空输入
                return;
            }

            updateList(boxCode);
        });
    </script>

    
</body>
</html> 